Page({
  data: {
    houseInfo: null,
    viewType: 1, // 1: 视频看房, 2: VR看房
    availableDates: [],
    timeSlots: [],
    selectedDate: '',
    selectedTime: '',
    contactInfo: {
      name: '',
      phone: ''
    }
  },

  onLoad(options) {
    this.getHouseInfo()
    this.getAvailableDates()
  },

  // 获取房源信息
  getHouseInfo() {
    // TODO: 调用API获取房源信息
    this.setData({
      houseInfo: {
        id: 1,
        title: '阳光小区 2室1厅',
        imageUrl: '/assets/images/house1.jpg',
        price: 2800
      }
    })
  },

  // 获取可预约时间
  getAvailableDates() {
    // 生成未来7天的日期
    const dates = []
    const today = new Date()
    
    for (let i = 0; i < 7; i++) {
      const date = new Date(today)
      date.setDate(today.getDate() + i)
      
      dates.push({
        date: this.formatDate(date),
        dateText: `${date.getMonth() + 1}月${date.getDate()}日`,
        weekDay: this.getWeekDay(date)
      })
    }

    // 生成时间段
    const slots = []
    for (let i = 9; i <= 17; i++) {
      slots.push({
        time: `${i}:00`,
        timeText: `${i}:00`
      })
    }

    this.setData({
      availableDates: dates,
      timeSlots: slots
    })
  },

  // 格式化日期
  formatDate(date) {
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
  },

  // 获取星期几
  getWeekDay(date) {
    const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    return weekDays[date.getDay()]
  },

  // 切换看房方式
  onViewTypeChange(e) {
    const type = parseInt(e.currentTarget.dataset.type)
    this.setData({ viewType: type })
  },

  // 选择日期
  onDateSelect(e) {
    const date = e.currentTarget.dataset.date
    this.setData({ selectedDate: date })
  },

  // 选择时间
  onTimeSelect(e) {
    const time = e.currentTarget.dataset.time
    this.setData({ selectedTime: time })
  },

  // 提交预约
  onSubmit() {
    const { selectedDate, selectedTime, contactInfo, viewType } = this.data

    if (!selectedDate || !selectedTime) {
      wx.showToast({
        title: '请选择看房时间',
        icon: 'none'
      })
      return
    }

    if (!contactInfo.name || !contactInfo.phone) {
      wx.showToast({
        title: '请填写联系信息',
        icon: 'none'
      })
      return
    }

    // TODO: 调用预约API
    wx.showLoading({
      title: '提交中'
    })

    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '预约成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            wx.navigateBack()
          }, 2000)
        }
      })
    }, 1500)
  }
}) 